import time

import page
from base.base import Base


class PageBackProduct(Base):
    # 点击一级标题：商品
    def page_click_first_level_product(self):
        self.base_click_element(page.back_first_level_product)

    # 点击二级标题：商品管理
    def page_click_second_level_product_management(self):
        self.base_click_element(page.back_second_level_product_management)

    # 切换到商品管理对应的iframe
    def page_switch_product_management_iframe(self):
        self.base_switch_to_iframe(page.back_product_management_iframe)

    # 输入商品名称
    def page_input_product_name(self, product_name):
        self.base_in_element_input(page.back_product_name, product_name)

    # 点击商品分类下拉框
    def page_click_product_category(self):
        self.base_click_element(page.back_product_category)

    # 选择商品分类：手机
    def page_select_product_category_tel(self):
        self.base_click_element(page.back_product_category_tel)

    # 输入商品描述
    def page_input_product_description(self, description):
        self.base_in_element_input(page.back_product_description, description)

    # 输入商品型号
    def page_input_product_model(self, model):
        self.base_in_element_input(page.back_product_model, model)

    # 点击品牌下拉框
    def page_click_product_brand(self):
        self.base_click_element(page.back_product_brand)

    # 选择品牌：vivo
    def page_select_product_brand_vivo(self):
        self.base_click_element(page.back_product_brand_vivo)

    def page_scroll_to_cover_image(self):
        self.base_scroll_to_element(page.back_product_cover_image_btn)

    # 点击生产地下拉框
    def page_click_product_origin(self):
        self.base_click_element(page.back_product_origin)

    # 选择生产地：山西
    def page_select_product_origin_shanxi(self):
        self.base_click_element(page.back_product_origin_shanxi)

    # 输入库存单位
    def page_input_product_stock_unit(self, unit):
        self.base_in_element_input(page.back_product_stock_unit, unit)

    # 输入购买赠送积分比例
    def page_input_product_point_ratio(self, ratio):
        self.base_in_element_input(page.back_product_point_ratio, ratio)

    # 输入排序权重
    def page_input_product_sort_weight(self, weight):
        self.base_in_element_input(page.back_product_sort_weight, weight)

    # 点击商品类型下拉框
    def page_click_product_type(self):
        self.base_click_element(page.back_product_type)

    # 选择商品类型：快递
    def page_select_product_type_express(self):
        self.base_click_element(page.back_product_type_express)

    # 点击封面图片上传按钮
    def page_click_product_cover_image_btn(self):
        self.base_click_element(page.back_product_cover_image_btn)

    # 切换到封面图片iframe
    def page_switch_product_cover_image_iframe(self):
        self.base_switch_to_iframe(page.back_product_cover_image_iframe)

    # 上传封面图片（传入图片路径）
    def page_upload_product_cover_image(self, cover_image_path):
        self.base_in_element_input(page.back_product_cover_image, cover_image_path)

    # 点击封面图片上传确认
    def page_confirm_product_cover_image_upload(self):
        self.base_click_element(page.back_product_cover_image_upload)

    # 点击封面图片最终确认
    def page_submit_product_cover_image(self):
        self.base_click_element(page.back_product_cover_image_submit)

    def page_scroll_to_final(self):
        self.base_scroll_to_element(page.back_product_final)

    # 切换到商品规格标签页
    def page_switch_to_product_specs_tab(self):
        self.base_click_element(page.back_product_specs)

    # 点击添加其他商品规格按钮
    def page_click_add_other_product_specs(self):
        self.base_click_element(page.back_product_add_other_specs)

    # 点击商品规格添加一行按钮
    def page_click_add_product_spec_row(self):
        self.base_click_element(page.back_product_add_spec_row)

    # 输入新增规格的名称
    def page_input_add_other_specs_name(self, spec_name):
        self.base_in_element_input(page.back_product_add_other_specs_name, spec_name)

    # 输入第1行新增规格的值
    def page_input_add_other_specs_value_row1(self, spec_value):
        self.base_in_element_input(page.back_product_add_other_specs_number_row1, spec_value)

    # 输入第2行新增规格的值
    def page_input_add_other_specs_value_row2(self, spec_value):
        self.base_in_element_input(page.back_product_add_other_specs_number_row2, spec_value)

    # 输入第1行商品售价
    def page_input_product_selling_price_row1(self, price):
        self.base_in_element_input(page.back_product_selling_price_row1, price)

    # 输入第2行商品售价
    def page_input_product_selling_price_row2(self, price):
        self.base_in_element_input(page.back_product_selling_price_row2, price)

    # 输入第1行商品原价
    def page_input_product_original_price_row1(self, original_price):
        self.base_in_element_input(page.back_product_original_price_row1, original_price)

    # 输入第2行商品原价
    def page_input_product_original_price_row2(self, original_price):
        self.base_in_element_input(page.back_product_original_price_row2, original_price)

    # 输入第1行商品起购数
    def page_input_product_min_purchase_row1(self, min_num):
        self.base_in_element_input(page.back_product_min_purchase_row1, min_num)

    # 输入第2行商品起购数
    def page_input_product_min_purchase_row2(self, min_num):
        self.base_in_element_input(page.back_product_min_purchase_row2, min_num)

    # 输入第1行商品限购数
    def page_input_product_max_purchase_row1(self, max_num):
        self.base_in_element_input(page.back_product_max_purchase_row1, max_num)

    # 输入第2行商品限购数
    def page_input_product_max_purchase_row2(self, max_num):
        self.base_in_element_input(page.back_product_max_purchase_row2, max_num)

    # 输入第1行商品重量
    def page_input_product_weight_row1(self, weight):
        self.base_in_element_input(page.back_product_weight_row1, weight)

    # 输入第2行商品重量
    def page_input_product_weight_row2(self, weight):
        self.base_in_element_input(page.back_product_weight_row2, weight)

    # 输入第1行商品体积
    def page_input_product_volume_row1(self, volume):
        self.base_in_element_input(page.back_product_volume_row1, volume)

    # 输入第2行商品体积
    def page_input_product_volume_row2(self, volume):
        self.base_in_element_input(page.back_product_volume_row2, volume)

    # 输入第1行商品编码
    def page_input_product_code_row1(self, code):
        self.base_in_element_input(page.back_product_code_row1, code)

    # 输入第2行商品编码
    def page_input_product_code_row2(self, code):
        self.base_in_element_input(page.back_product_code_row2, code)

    # 输入第1行商品条形码
    def page_input_product_barcode_row1(self, barcode):
        self.base_in_element_input(page.back_product_barcode_row1, barcode)

    # 输入第2行商品条形码
    def page_input_product_barcode_row2(self, barcode):
        self.base_in_element_input(page.back_product_barcode_row2, barcode)

    # 切换到商品参数标签页
    def page_switch_to_product_parameters_tab(self):
        self.base_click_element(page.back_product_parameters)

    # 点击商品参数添加一行按钮
    def page_click_add_product_param_row(self):
        self.base_click_element(page.back_product_add_param_row)

    # 点击展示范围下拉框
    def page_click_product_display_scope(self):
        self.base_click_element(page.back_product_display_scope)

    # 选择展示范围：详情
    def page_select_product_display_scope_detail(self):
        self.base_click_element(page.back_product_display_scope_detail)

    # 输入参数名称
    def page_input_product_param_name(self, param_name):
        self.base_in_element_input(page.back_product_param_name, param_name)

    # 输入参数值
    def page_input_product_param_value(self, param_value):
        self.base_in_element_input(page.back_product_param_value, param_value)

    # 切换到商品相册/视频标签页
    def page_switch_to_product_media_tab(self):
        self.base_click_element(page.back_product_media)

    # 点击商品相册上传按钮
    def page_click_product_album_btn(self):
        self.base_click_element(page.back_product_album_btn)

    def page_upload_product_album(self, album_image_paths):
        self.base_in_element_input(page.back_product_cover_image, album_image_paths)

    # 点击商品视频上传按钮
    def page_click_product_video_btn(self):
        self.base_click_element(page.back_product_video_btn)

    def page_upload_product_video(self, video_path):
        self.base_in_element_input(page.back_product_cover_image, video_path)

    # 切换到手机详情标签页
    def page_switch_to_product_mobile_detail_tab(self):
        self.base_click_element(page.back_product_mobile_detail)

    # 点击添加手机详情按钮
    def page_click_add_mobile_detail(self):
        self.base_click_element(page.back_product_add_mobile_detail)

    # 点击手机详情上传图片按钮
    def page_click_tel_detail_upload_image(self):
        self.base_click_element(page.back_product_tel_upload_image)

    # 上传手机详情图片（传入图片路径）
    def page_upload_tel_detail_image(self, tel_detail_image_path):
        self.base_in_element_input(page.back_product_cover_image, tel_detail_image_path)

    # 输入手机详情文本内容
    def page_input_tel_detail_text_content(self, text):
        self.base_in_element_input(page.back_product_tel_text_content, text)

    # 切换到电脑详情标签页
    def page_switch_to_product_pc_detail_tab(self):
        self.base_click_element(page.back_product_pc_detail)

    # 上传电脑详情图片（传入图片路径）
    def page_upload_pc_detail_image(self, pc_detail_image_path):
        self.base_switch_to_iframe(page.back_product_pc_detail_image_frame)
        self.base_in_element_input(page.back_product_pc_detail_image, pc_detail_image_path)

    # 切换到电脑详情iframe
    def page_switch_pc_detail_iframe(self):
        self.base_switch_to_iframe(page.back_product_pc_detail_iframe)

    def page_switch_product_video_iframe(self):
        self.base_switch_to_iframe(page.back_product_video_iframe)

    def page_switch_product_video_submit(self):
        self.base_click_element(page.back_product_video_submit)

    def page_tel_select(self):
        self.base_click_element(page.back_product_tel_select)

    # 输入电脑详情文本内容
    def page_input_pc_detail_text(self, text):
        self.base_in_element_input(page.back_product_pc_detail_text, text)

    def page_product_submit(self):
        self.base_click_element(page.back_product_submit)

    def page_product_update(self):
        self.base_click_element(page.get_product_id()["back_update_product_btn"])

    def page_product_add(self):
        self.base_click_element(page.back_add_btn)

    # 回到父iframe
    def page_switch_parent_frame(self):
        self.base_switch_to_parent_frame()

    def page_switch_to_default_content(self):
        self.base_switch_to_default_content()

    def page_input_clear(self):
        self.base_clear_input(page.back_product_description)
    # 获得截屏
    def page_get_screenshot(self):
        self.base_get_img()

    def page_into_product(self):
        self.page_click_first_level_product()
        time.sleep(0.5)
        self.page_click_second_level_product_management()
        time.sleep(0.5)
        self.page_switch_product_management_iframe()
        time.sleep(0.5)


    def page_product_config(self, product_name, product_description, product_model,
                            stock_unit, point_ratio, sort_weight, cover_image_path):
        self.page_input_product_name(product_name)
        self.page_click_product_category()
        self.page_select_product_category_tel()
        self.page_input_product_description(product_description)
        self.page_input_product_model(product_model)
        self.page_click_product_brand()
        self.page_select_product_brand_vivo()
        self.page_scroll_to_cover_image()
        self.page_click_product_origin()
        self.page_select_product_origin_shanxi()
        self.page_input_product_stock_unit(stock_unit)
        self.page_input_product_point_ratio(point_ratio)
        self.page_input_product_sort_weight(sort_weight)
        self.page_click_product_type()
        self.page_select_product_type_express()
        self.page_click_product_cover_image_btn()
        self.page_switch_product_cover_image_iframe()
        self.page_upload_product_cover_image(cover_image_path)
        self.page_confirm_product_cover_image_upload()
        time.sleep(1)
        self.page_switch_parent_frame()
        self.page_submit_product_cover_image()

    def page_product_specs(self, spec_name, spec_value_row1, spec_value_row2, selling_price_row1,
                           selling_price_row2, original_price_row1, original_price_row2,
                           min_purchase_row1, min_purchase_row2, max_purchase_row1, max_purchase_row2, weight_row1,
                           weight_row2, volume_row1, volume_row2, code_row1, code_row2, barcode_row1, barcode_row2):
        self.page_switch_to_product_specs_tab()

        # 点击添加其他商品规格按钮
        self.page_click_add_other_product_specs()
        # 点击商品规格添加一行按钮
        self.page_click_add_product_spec_row()
        # 输入新增规格的名称
        self.page_input_add_other_specs_name(spec_name)

        # 输入第1行新增规格的值
        self.page_input_add_other_specs_value_row1(spec_value_row1)
        # 输入第1行商品售价
        self.page_input_product_selling_price_row1(selling_price_row1)
        # 输入第1行商品原价
        self.page_input_product_original_price_row1(original_price_row1)
        # 输入第1行商品起购数
        self.page_input_product_min_purchase_row1(min_purchase_row1)
        # 输入第1行商品限购数
        self.page_input_product_max_purchase_row1(max_purchase_row1)
        # 输入第1行商品重量
        self.page_input_product_weight_row1(weight_row1)
        # 输入第1行商品体积
        self.page_input_product_volume_row1(volume_row1)
        # 输入第1行商品编码
        self.page_input_product_code_row1(code_row1)
        # 输入第1行商品条形码
        self.page_input_product_barcode_row1(barcode_row1)

        # 输入第2行新增规格的值
        self.page_input_add_other_specs_value_row2(spec_value_row2)
        # 输入第2行商品售价
        self.page_input_product_selling_price_row2(selling_price_row2)
        # 输入第2行商品原价
        self.page_input_product_original_price_row2(original_price_row2)
        # 输入第2行商品起购数
        self.page_input_product_min_purchase_row2(min_purchase_row2)
        # 输入第2行商品限购数
        self.page_input_product_max_purchase_row2(max_purchase_row2)
        # 输入第2行商品重量
        self.page_input_product_weight_row2(weight_row2)
        # 输入第2行商品体积
        self.page_input_product_volume_row2(volume_row2)
        # 输入第2行商品编码
        self.page_input_product_code_row2(code_row2)
        # 输入第2行商品条形码
        self.page_input_product_barcode_row2(barcode_row2)

    def page_product_parameters(self, param_name, param_value):
        self.page_switch_to_product_parameters_tab()

        # 点击商品参数添加一行按钮
        self.page_click_add_product_param_row()

        # 点击展示范围下拉框
        self.page_click_product_display_scope()

        # 选择展示范围：详情
        self.page_select_product_display_scope_detail()

        # 输入参数名称
        self.page_input_product_param_name(param_name)

        # 输入参数值
        self.page_input_product_param_value(param_value)

    def page_product_media(self, album_image_paths, video_path):
        self.page_switch_to_product_media_tab()

        # 点击商品相册上传按钮
        self.page_click_product_album_btn()
        self.page_switch_product_cover_image_iframe()
        self.page_upload_product_album(album_image_paths)
        self.page_confirm_product_cover_image_upload()
        time.sleep(1)
        self.page_switch_parent_frame()
        self.page_submit_product_cover_image()

        # 点击商品视频上传按钮
        self.page_click_product_video_btn()
        self.page_switch_product_video_iframe()
        self.page_upload_product_video(video_path)
        self.page_confirm_product_cover_image_upload()
        time.sleep(1)
        self.page_switch_parent_frame()
        self.page_switch_product_video_submit()

    def page_product_mobile(self, tel_detail_image_path, tel_detail_text):
        self.page_switch_to_product_mobile_detail_tab()

        # 点击添加手机详情按钮
        self.page_click_add_mobile_detail()

        # 点击手机详情上传图片按钮
        self.page_click_tel_detail_upload_image()

        # 上传手机详情图片（传入图片路径）
        self.page_switch_product_cover_image_iframe()
        self.page_tel_select()
        time.sleep(1)
        self.page_upload_tel_detail_image(tel_detail_image_path)
        self.page_confirm_product_cover_image_upload()
        time.sleep(1)
        self.page_switch_parent_frame()
        self.page_submit_product_cover_image()
        # 输入手机详情文本内容
        self.page_input_tel_detail_text_content(tel_detail_text)

    def page_product_pc(self, pc_detail_text, pc_detail_image_path):
        # 切换到电脑详情标签页
        self.page_switch_to_product_pc_detail_tab()

        # 上传电脑详情图片（传入图片路径）
        # self.page_upload_pc_detail_image(pc_detail_image_path)

        # 切换到电脑详情iframe
        self.page_switch_pc_detail_iframe()

        # 输入电脑详情文本内容
        self.page_input_pc_detail_text(pc_detail_text)
        self.page_switch_parent_frame()

    def page_product(self, product_data):
        self.page_product_add()
        self.page_product_config(
            product_name=product_data["product_name"],
            product_description=product_data["product_description"],
            product_model=product_data["product_model"],
            stock_unit=product_data["stock_unit"],
            point_ratio=product_data["point_ratio"],
            sort_weight=product_data["sort_weight"],
            cover_image_path=product_data["cover_image_path"]
        )

        self.page_product_specs(
            spec_name=product_data["spec_name"],
            spec_value_row1=product_data["spec_value_row1"],
            spec_value_row2=product_data["spec_value_row2"],
            selling_price_row1=product_data["selling_price_row1"],
            selling_price_row2=product_data["selling_price_row2"],
            original_price_row1=product_data["original_price_row1"],
            original_price_row2=product_data["original_price_row2"],
            min_purchase_row1=product_data["min_purchase_row1"],
            min_purchase_row2=product_data["min_purchase_row2"],
            max_purchase_row1=product_data["max_purchase_row1"],
            max_purchase_row2=product_data["max_purchase_row2"],
            weight_row1=product_data["weight_row1"],
            weight_row2=product_data["weight_row2"],
            volume_row1=product_data["volume_row1"],
            volume_row2=product_data["volume_row2"],
            code_row1=product_data["code_row1"],
            code_row2=product_data["code_row2"],
            barcode_row1=product_data["barcode_row1"],
            barcode_row2=product_data["barcode_row2"]
        )

        self.page_product_parameters(
            param_name=product_data["param_name"],
            param_value=product_data["param_value"]
        )

        self.page_product_media(
            album_image_paths=product_data["album_image_paths"],
            video_path=product_data["video_path"]
        )

        self.page_product_mobile(
            tel_detail_image_path=product_data["tel_detail_image_path"],
            tel_detail_text=product_data["tel_detail_text"]
        )

        self.page_product_pc(
            pc_detail_text=product_data["pc_detail_text"],
            pc_detail_image_path=product_data["pc_detail_image_path"]
        )
        self.page_product_submit()
        time.sleep(1)

    def page_update_product(self, product_data):
        self.page_product_update()
        self.page_input_clear()
        self.page_input_product_description(product_data["product_description_alert"])
        self.page_scroll_to_final()
        self.page_product_submit()
        time.sleep(1)

    def page_del_product(self):
        self.base_click_element(page.get_product_id()['back_product_select'])
        self.base_click_element(page.back_delete_checked)
        self.base_click_element(page.product_confirm_btn)
        time.sleep(1)

